const { JSDOM } = require('jsdom')

const ChdGradeOrder = ({ res, response }) => {
  res.setHeader('Content-Type', 'application/json')
  try {
    const dom = new JSDOM(response)
    const document = dom.window.document
    const list = []

    const domList = [...document.querySelector('tbody').querySelectorAll('tr')]

    domList.forEach((tr, i) => {
      if (i === 0) {
        return
      }
      const [td1, td2, td3, td4, td5, td6] = [...tr.querySelectorAll('td')]

      list.push({
        date: td1.innerHTML.replace(/<br>/, ' '),
        id: td2.innerHTML,
        orderName: td4.innerHTML.split('<br>').filter(item => item),
        orderCode: td5.innerHTML.split('<br>').filter(item => item),
        price: td6.innerHTML
      })
    })

    return JSON.stringify({
      data: list,
      success: true
    })
  } catch (error) {
    return JSON.stringify({
      success: false
    })
  }
}

module.exports = {
  ChdGradeOrder
}
